#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e4 + 5;
const int M = 5e4 + 5;

int n, m;
int f[N];
int num;
int st[N][16], dep[N], w[N];
void init(int n) { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool merge(int x, int y, int z) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return false;
  ++num;
  f[fx] = st[fx][0] = num;
  f[fy] = st[fy][0] = num;
  w[num] = z;
  return true;
}
void init_st() {
  dep[num] = 0;
  per(i, num - 1, 1) dep[i] = dep[st[i][0]] + 1;
  rep(j, 1, 14) rep(i, 1, num) st[i][j] = st[st[i][j - 1]][j - 1];
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);

  int d = dep[u] - dep[v];
  per(j, 14, 0) if (d & (1 << j)) u = st[u][j];
  if (u == v) return u;

  per(j, 14, 0) if (st[u][j] != st[v][j]) u = st[u][j], v = st[v][j];
  return st[u][0];
}
struct Edge {
  int u, v, w;
  bool operator<(Edge o) const { return w > o.w; }
} e[M];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) cin >> e[i].u >> e[i].v >> e[i].w;
  sort(e + 1, e + m + 1);
  init(n * 2);
  num = n;
  rep(i, 1, m) merge(e[i].u, e[i].v, e[i].w);
  rep(i, 1, num) {
    if (f[i] == i) st[i][0] = num + 1;
  }
  num++;
  w[num] = -1;
  st[num][0] = num;
  init_st();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int x = lca(u, v);
    cout << w[x] << endl;
  }
  return 0;
}